home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 742 / colorswitch / src / colorswitch.c next >
C/C++ Source or Header  |  1995-03-18  |  6KB  |  233 lines

  1. /*
  2.  *  Gadget source generated with GadToolsBox V1.3
  3.  *  which is (c) Copyright 1991,92 Jaba Development
  4.  *
  5.  *  NB: edited.
  6.  *
  7.  *  ColorSwitch
  8.  *
  9.  *  Lets you change palettes - 1.3, 2.0 and your own - at the click of
  10.  *  a mouse. Based on an idea by Guido Wegener.
  11.  *
  12.  *  Martin W. Scott, 28 June 1992.
  13.  */
  14. #include <exec/types.h>
  15. #include <intuition/intuition.h>
  16. #include <intuition/classes.h>
  17. #include <intuition/classusr.h>
  18. #include <intuition/imageclass.h>
  19. #include <intuition/gadgetclass.h>
  20. #include <intuition/preferences.h>
  21. #include <libraries/gadtools.h>
  22. #include <graphics/displayinfo.h>
  23. #include <graphics/gfxbase.h>
  24. #include <clib/exec_protos.h>
  25. #include <clib/intuition_protos.h>
  26. #include <clib/gadtools_protos.h>
  27. #include <clib/graphics_protos.h>
  28. #include <string.h>
  29.  
  30. #include <pragmas/exec_pragmas.h>
  31. #include <pragmas/intuition_pragmas.h>
  32. #include <pragmas/gadtools_pragmas.h>
  33. #include <pragmas/graphics_pragmas.h>
  34.  
  35. struct IntuitionBase *IntuitionBase;
  36. struct GadToolsBase *GadToolsBase;
  37.  
  38. UWORD palettes[3][4] = {
  39.     { 0x05a, 0xfff, 0x002, 0xf80 },    /* WB1.x prefs */
  40.     { 0xaaa, 0x001, 0xfff, 0x57a }    /* WB2.0 prefs */
  41.     /* user prefs */
  42. };
  43.  
  44. #include "colorswitch.h"
  45.  
  46. struct Screen        *Scr = NULL;
  47. APTR                  VisualInfo = NULL;
  48. struct Window        *SwitchWnd = NULL;
  49. struct Gadget        *SwitchGList = NULL;
  50. struct Gadget        *SwitchGadgets[1];
  51. UWORD                 SwitchLeft = 460;
  52. UWORD                 SwitchTop = 18;
  53. UWORD                 SwitchWidth = 125;
  54. UWORD                 SwitchHeight = 33;
  55. UBYTE                *SwitchWdt = (UBYTE *)"Colors";
  56.  
  57. WORD SwitchZoom[4] = {460, 18, 125, 0 };
  58.  
  59. UBYTE         *PaletteLabels[] = {
  60.     (UBYTE *)"WB 1.x",
  61.     (UBYTE *)"WB 2.0",
  62.     (UBYTE *)"Prefs",
  63.     NULL };
  64.  
  65. struct TextAttr topaz8 = {
  66.     ( STRPTR )"topaz.font", 8, 0x00, 0x00 };
  67.  
  68. int SetupScreen( void )
  69. {
  70.     if ( ! ( Scr = LockPubScreen((UBYTE * )"Workbench" )))
  71.         return( 1L );
  72.  
  73.     if ( ! ( VisualInfo = GetVisualInfo( Scr, TAG_DONE )))
  74.         return( 2L );
  75.  
  76.     return( 0L );
  77. }
  78.  
  79. void CloseDownScreen( void )
  80. {
  81.     if ( VisualInfo ) {
  82.         FreeVisualInfo( VisualInfo );
  83.         VisualInfo = NULL;
  84.     }
  85.  
  86.     if ( Scr        ) {
  87.         UnlockPubScreen( NULL, Scr );
  88.         Scr = NULL;
  89.     }
  90. }
  91.  
  92. int OpenSwitchWindow( void )
  93. {
  94.     struct NewGadget     ng;
  95.     struct Gadget       *g;
  96.     UWORD               offx, offy;
  97.  
  98.     offx = Scr->WBorLeft;
  99.     offy = Scr->WBorTop + Scr->RastPort.TxHeight + 1;
  100.     SwitchZoom[3] = offy;
  101.  
  102.     if ( ! ( g = CreateContext( &SwitchGList )))
  103.         return( 1L );
  104.  
  105.     ng.ng_LeftEdge        =    offx + 20;
  106.     ng.ng_TopEdge         =    offy + 2;
  107.     ng.ng_Width           =    17;
  108.     ng.ng_Height          =    9;
  109.     ng.ng_GadgetText      =    NULL;
  110.     ng.ng_TextAttr        =    &topaz8;
  111.     ng.ng_GadgetID        =    GD_Palette;
  112.     ng.ng_Flags           =    PLACETEXT_RIGHT;
  113.     ng.ng_VisualInfo      =    VisualInfo;
  114.  
  115.     g = CreateGadget( MX_KIND, g, &ng, GTMX_Active, 2, GTMX_Labels, &PaletteLabels[0], TAG_DONE );
  116.  
  117.     SwitchGadgets[ 0 ] = g;
  118.  
  119.     if ( ! g )
  120.         return( 2L );
  121.  
  122.     if ( ! ( SwitchWnd = OpenWindowTags( NULL,
  123.                     WA_Left,          SwitchLeft,
  124.                     WA_Top,           SwitchTop,
  125.                     WA_Width,         SwitchWidth,
  126.                     WA_Height,        SwitchHeight + offy,
  127.                     WA_IDCMP,         MXIDCMP|IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW,
  128.                     WA_Flags,         WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH,
  129.                     WA_Gadgets,       SwitchGList,
  130.                     WA_Title,         SwitchWdt,
  131.                     WA_ScreenTitle,   "ColorSwitch",
  132.             WA_Zoom,          SwitchZoom,
  133.                     TAG_DONE )))
  134.         return( 4L );
  135.  
  136.     GT_RefreshWindow( SwitchWnd, NULL );
  137.  
  138.     return( 0L );
  139. }
  140.  
  141. void CloseSwitchWindow( void )
  142. {
  143.     if ( SwitchWnd        ) {
  144.         CloseWindow( SwitchWnd );
  145.         SwitchWnd = NULL;
  146.     }
  147.  
  148.     if ( SwitchGList      ) {
  149.         FreeGadgets( SwitchGList );
  150.         SwitchGList = NULL;
  151.     }
  152. }
  153.  
  154. void CloseLibs()
  155. {
  156.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  157.     if (GadToolsBase) CloseLibrary(GadToolsBase);
  158. }
  159.  
  160. BOOL OpenLibs()
  161. {
  162.     if ((IntuitionBase = (void *)OpenLibrary("intuition.library",37L)) &&
  163.         (GadToolsBase =  (void *)OpenLibrary("gadtools.library",37L)))
  164.         return TRUE;
  165.  
  166.     CloseLibs();
  167.     return FALSE;
  168. }
  169.  
  170. void GetSysPalette(UWORD *palette)
  171. {
  172.     struct Preferences prefs;
  173.  
  174.     GetPrefs(&prefs, sizeof(prefs));
  175.     *palette++ = prefs.color0;
  176.     *palette++ = prefs.color1;
  177.     *palette++ = prefs.color2;
  178.     *palette = prefs.color3;
  179. }
  180.  
  181. void SetSysPalette(UWORD *palette)
  182. {
  183.     struct Preferences prefs;
  184.  
  185.     GetPrefs(&prefs, sizeof(prefs));
  186.     prefs.color0 = *palette++;
  187.     prefs.color1 = *palette++;
  188.     prefs.color2 = *palette++;
  189.     prefs.color3 = *palette;
  190.     SetPrefs(&prefs, sizeof(prefs), TRUE);
  191. }
  192.  
  193. void _main()
  194. {
  195.     if (!OpenLibs())
  196.         return;
  197.  
  198.     if (!SetupScreen())
  199.     {
  200.         if (!OpenSwitchWindow())
  201.         {
  202.             GetSysPalette(&palettes[2][0]);
  203.  
  204.             for (;;)
  205.                 {
  206.                 struct IntuiMessage *msg;
  207.                 ULONG class;
  208.                 UWORD code;
  209.  
  210.                 WaitPort(SwitchWnd->UserPort);
  211.                 while (msg = GT_GetIMsg(SwitchWnd->UserPort))
  212.                 {
  213.                     class = msg->Class;
  214.                     code = msg->Code;
  215.                     GT_ReplyIMsg(msg);
  216.  
  217.                     if (class == CLOSEWINDOW)
  218.                         goto byebye; /* goto... naughty! */
  219.                     else if (class == MXIDCMP)
  220.                         SetSysPalette(&palettes[code][0]);
  221.                 }
  222.  
  223.             } /* for (;;) */
  224.  
  225.             byebye:
  226.             CloseSwitchWindow();
  227.             SetSysPalette(&palettes[2][0]);
  228.         }
  229.         CloseDownScreen();
  230.     }
  231.     CloseLibs();
  232.  
  233. } /* main */